<template>
<!-- 添加商品页 -->

<div class="maxBox">
    <!-- 标题组件 -->
    <panel title="商品添加">
        <div class="content">

            <el-form ref="form" :model="formData" label-width="80px">
                <el-form-item label="商品名称">
                    <el-input v-model="formData.name" placeholder="商品名称"></el-input>
                </el-form-item>
                <el-form-item label="商品分类">
                    <el-select v-model="formData.category" placeholder="请选择商品分类">
                        <el-option v-for="(item,index) in cateList" :key="index" :label="item.cateName" :value="item.cateName"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="商品价格">
                    <el-input-number v-model="formData.price" :min="0" label="商品价格"></el-input-number>
                </el-form-item>

                <el-form-item label="商品图片">

                    <el-upload class="avatar-uploader" action="http://localhost:5000/goods/goods_img_upload" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>

                <el-form-item label="商品描述" prop="desc">
                    <el-input type="textarea" v-model="formData.goodsDesc"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="addBtn">添加商品</el-button>
                    <el-button>取消</el-button>
                </el-form-item>
            </el-form>

        </div>
    </panel>
</div>
</template>

<script>
// 导入公共标题组件
import Panel from "../../components/Panel.vue";
import {
    goodsCateList,
    goodsAdd
} from '@/api/goods.js'

export default {
    data() {
        return {
            formData: {
                name: '',
                category: '',
                price: '',
                imgUrl: '',
                goodsDesc: '',
            },
            imageUrl: '',
            cateList: [], //分类列表
        };
    },

    // 注册组件
    components: {
        Panel,
    },
    methods: {

        // 添加按钮
        async addBtn() {
            // substr截取图片文件名
            let index = this.imageUrl.lastIndexOf('/'); //找到最后的/
            let str = this.imageUrl.substr(index + 1);
            this.formData.imgUrl = str;

            let res = await goodsAdd(this.formData);
            console.log(res);

            // 如果成功了 code=0，跳转到商品列表里
            if (res.data.code === 0) {
                setTimeout(() => {
                    this.$router.push('/goods-list')
                }, 1100);
            }
        },
        // 上传图片成功 回调
        handleAvatarSuccess(res) {
            console.log(res);
            this.imageUrl = 'http://localhost:5000' + res.imgUrl
        },
        // 上传之前 回调
        beforeAvatarUpload() {
          goodsCateList()
        },
        // 请求所有分类列表
        async goodsCateList() {
            let res = await goodsCateList();
            console.log(res);
            this.cateList = res.data.categories;
        }
    },

    //  请求所有分类列表
    created() {
        this.goodsCateList();
    }
};
</script>

<style lang="less" scoped>
.maxBox {
    width: 100%;
    height: 100%;

    .contentBox {
        margin: 12px auto 0 auto;
        width: 98%;
        background: #fff;
    }
}

.inp {
    display: flex;
    margin: 0px 0 20px 18px;
    width: 550px;

    p {
        width: 100px;
        line-height: 40px;
    }
}

.inp1 {
    display: flex;
    margin: 20px 0 20px 18px;
    width: 322px;

    p {
        width: 84px;
        line-height: 40px;
    }
}

.jishu {
    display: flex;
    margin: 20px 0 20px 18px;

    p {
        width: 85px;
        line-height: 40px;
    }
}

.uploadImg {
    display: flex;
    margin: 30px 0 20px 20px;
}

.el-upload {
    margin-left: 5px;
}

.goodsMS {
    display: flex;
    margin: 30px 0 20px 20px;
    width: 410px;

    p {
        width: 100px;
        line-height: 30px;
    }
}

.tjBtn {
    padding-bottom: 5px;
    margin: 30px 0 20px 102px;
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}

.el-icon-plus {
    border: 1px dashed #8c939d;
}
</style>
